import logging
import tkinter as tk
import tkinter.messagebox
from tkinter import *
import MainUI
import CSCommunication
import ctypes
import sys

##############
#author:张子路#
##############

class LoginStart():
    def __init__(self):
        try:
            self.window=tk.Tk()         #创建登录界面
            self.entryVar1=tk.StringVar()
            self.entryVar2=tk.StringVar()
        except Exception as e:
            logging.error("登录界面创建出错 {0}".format(e), exc_info=True)
        try:
            self.Communcate=CSCommunication.CSCommunication()
        except Exception as e:
            logging.error("连接文件CSCommunication.py失败 {0}".format(e), exc_info=True)

    def is_admin(self):
        try:
            return ctypes.windll.shell32.IsUserAnAdmin()         #检查是否是以管理员权限运行
        except:
            return False

    def start(self):
        try:
            if not self.is_admin():
                ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)        #不是管理员时向用户请求管理员权限
            else:
                self.LoginUI()        #以管理员权限运行时创建登录界面
        except Exception as e:
            logging.error("获取权限失败 {0}".format(e), exc_info=True)

    def quit(self):
        sys.exit()

    def LoginUI(self):
        try:               #登录界面
            self.window.title("登录窗口")
            self.window.geometry('400x300') #设置窗口尺寸


            label1=tk.Label(self.window,text='User name:',width=20,height=3)
            label1.place(x=40,y=50)

            entry1=tk.Entry(self.window,textvariable=self.entryVar1)
            entry1.place(x=160,y=70)
            label2=tk.Label(self.window,text='Password:',width=20,height=3)
            label2.place(x=40,y=100)
            entry2=tk.Entry(self.window,textvariable=self.entryVar2,show='*')
            entry2.place(x=160,y=120)
            button1=tk.Button(self.window,text='登录',command=self.login)
            button1.place(x=150,y=150)
            button2=tk.Button(self.window,text='取消',command=lambda:self.quit())
            button2.place(x=250,y=150)
            self.window.mainloop()
        except Exception as e:
            logging.error("登录界面创建出错 {0}".format(e), exc_info=True)


    def login(self):
        try:
            user_name=self.entryVar1.get()
            password=self.entryVar2.get()
            if user_name=='' or password=='':
                tk.messagebox.showinfo(title='提示',message='用户名或密码不能为空')       #密码不能为空
            else:
                LoginResult=self.Communcate.Login(user_name,password)           #与前端通信得到登录结果
                if LoginResult==1:
                    tk.messagebox.showinfo(title='提示',message='登录成功')
                    self.window.destroy()
                    userui=MainUI.UserUI()            #生成主界面
                    userui.Start()
                else:
                    tk.messagebox.showinfo(title='提示',message='用户名或密码错误')
        except Exception as e:
            logging.error("登录界面跳转主界面出错 {0}".format(e), exc_info=True)

if __name__ == '__main__':
    login=LoginStart()
    login.start()